$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/increRecord/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'recordId', index: "recordId", key: true,hidden:true },
            { label: '用户名', name: 'recordUsername'},
            { label: '手机号', name: 'recordPhone' },
            { label: '增值类型', name: 'type', formatter: function(value, options, row){
                if (value == 'loan')
                    return '贷款申请';
                else
                    return '信用卡申请';
            }},
            { label: '使用通道', name: 'td', index: "td"},
            { label: '创建时间', name: 'createTime', index: "createTime"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    
    $("#select_type").change(function (e) {
        var type = $(this).val();
        var urlStr = '';
        if (type == '1'){
            // urlStr = '/platform/manage/increRecord/list';
        }else if (type == 'loan'){
            // urlStr = '/platform/manage/increRecord/list?type='+type;
            vm.loanListData();
        }else if (type == 'card'){
            // urlStr = '/platform/manage/increRecord/list?type='+type;
            vm.cardListData();
        }
        // $("#jqGrid").jqGrid('setGridParam',{
        //     url:urlStr
        // }).trigger('reloadGrid');
    });

    // $("#data_type").change(function (e) {
    //     var type = $(this).val();
    //     var urlStr = '';
    //     var selectType = $("#select_type").val();
    //     if (type == '-1'){
    //         urlStr = '/platform/manage/increRecord/list?type='+selectType;
    //     }else{
    //         urlStr = '/platform/manage/increRecord/list?type='+selectType+'&typeId='+type;
    //     }
    //     console.log(urlStr);
    //     $("#jqGrid").jqGrid('setGridParam',{
    //         url:urlStr
    //     }).trigger('reloadGrid');
    // });

    $("#startDate").flatpickr({
        dateFormat:'Ymd',
        "locale":"zh"
    });
    $("#endDate").flatpickr({
        dateFormat:'Ymd',
        "locale":"zh"
    });
    // $("#endDate").change(function (e) {
    //     var startValue = $("#startDate").val();
    //     var endValue = $(this).val();
    //     var queryData = {};
    //     queryData['startDate'] = startValue;
    //     queryData['endDate'] = endValue;
    //     var urlStr = "/platform/manage/increRecord/list";
    //     var type = $("#select_type").val();
    //     if (type != '1')
    //         queryData['type'] = type;
    //     var dataType = $("#data_type").val();
    //     if (dataType != '-1')
    //         queryData['typeId'] = dataType;
    //     console.log("queryData",queryData);
    //     $("#jqGrid").jqGrid('setGridParam',{
    //         url:urlStr,
    //         postData:queryData,
    //     }).trigger('reloadGrid');
    // });

});

var vm = new Vue({
    el:"#app",
    data:{
        showList:true,
        record:{},
        title:'',
        cardList:[],
        loanList:[],
        selectType:'1',
        dataTypeItem:[],
        isCard:false,
        isLoan:false
    },
    methods:{
        loanListData:function () {
            $.ajax({
                url:"/platform/manage/increRecord/loanList",
                dataType:"JSON",
                success:function (result) {
                    console.log("data",result.result);
                    if (result.code == 200){
                        // vm.loanList = result.result;
                        vm.dataTypeItem = result.result;
                    }else
                        alert(result.message);
                },
                error:function (e) {
                    console.error("error",e);
                    alert("请求失败");
                }
            });
        },
        download:function () {
            var urlStr = "/platform/manage/increRecord/download?t="+Date.parse(new Date()) + "&searchStr="+JSON.stringify(vm.serachData());
            window.open(encodeURI(urlStr),"_blank");
        },
        cardListData:function () {
            $.ajax({
                url:"/platform/manage/increRecord/cardList",
                dataType:"JSON",
                success:function (result) {
                    console.log("data",result.result);
                    if (result.code == 200){
                        // vm.cardList = result.result;
                        vm.dataTypeItem = result.result;
                    }else
                        alert(result.message);
                },
                error:function (e) {
                    console.error("error",e);
                    alert("请求失败");
                }
            });
        },
        query:function () {
            $("#jqGrid").jqGrid('setGridParam',{
                datatype:'json',
                postData:{'searchStr':''+JSON.stringify(vm.serachData())+''},
                page:1
            }).trigger("reloadGrid");
        },
        reload:function () {
            vm.title = '';
            vm.showList = true;
            $("#startDate").val('');
            $('#endDate').val('');
            $("#select_type").val('1');
            $("#data_type").val('-1');
            $("#jqGrid").jqGrid('setGridParam',{}).trigger('reloadGrid');
        },
        serachData:function () {
            var queryData = {};
            var startValue = $("#startDate").val();
            if (startValue != null && startValue != '')
                queryData['startDate'] = startValue;
            var endValue = $('#endDate').val();
            if (endValue != null && endValue != '')
                queryData['endDate'] = endValue;
            var type = $("#select_type").val();
            if (type != null && type != '' && type != '1')
                queryData['type'] = type;
            var dataType = $("#data_type").val();
            if (dataType != null && dataType != '' && dataType != '-1')
                queryData['typeId'] = dataType;
            return queryData;
        }
    }
});